package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.IdentityCardBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.IdentityCardMapper;
import com.molichuxing.services.infrastructure.dto.request.create.IdentityCardCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.IdentityCardModifyDto;
import com.molichuxing.services.infrastructure.dto.response.IdentityCardDto;
import com.molichuxing.services.infrastructure.service.IdentityCardService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 
 * @author liyg
 * 用户身份证服务实现类
 * 2019-08-13
 * 
 */
@Service("identityCardService")
public class IdentityCardServiceImpl implements IdentityCardService{
	
	@Autowired
	private IdentityCardMapper identityCardMapper;

	/**
	 * 获取用户身份证信息
	 * 
	 * @param userId
	 *            用户id
	 * @return
	 */
	@Override
	public IdentityCardDto getIdentityCard(Integer userId) {
		
		IdentityCardBean identityCard = identityCardMapper.getById(userId);
		
		IdentityCardDto dto = Convert.toIdentityCardDto(identityCard);
		
		return dto;
	}

	/**
	 * 获取用户身份证信息
	 * 
	 * @param userIds
	 *            用户ids
	 * @return
	 */
	@Override
	public Map<Integer, IdentityCardDto> getIdentityCards(List<Integer> userIds) {
		if (null == userIds || userIds.size() <= 0) {
			return null;
		}

		List<IdentityCardBean> identityCardBeanList = identityCardMapper.getByIds(userIds);
		List<IdentityCardDto> identityCardDtoList = Convert.toIdentityCardDto(identityCardBeanList);
		if (null == identityCardDtoList || identityCardDtoList.size() <= 0) {
			return null;
		}

		Integer  initialCapacity  = identityCardDtoList.size();
		Map<Integer, IdentityCardDto> map = new HashMap<>(initialCapacity);
		for (IdentityCardDto beanDto : identityCardDtoList) {
			Integer id = beanDto.getId();
			map.put(id, beanDto);
		}

		return map;
	}
	
	 /**
	  * 保存用户身份证信息
	  * @param createDto
	  * @return
	  */	
	@Override
	public Boolean create(IdentityCardCreateDto createDto) {
		IdentityCardBean bean=new IdentityCardBean();
		BeanUtils.copyProperties(createDto, bean);
		int num = identityCardMapper.create(bean);
		return num==1;
	}
	
	/**
	 * 修改用户身份证号信息
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyIdNumber(IdentityCardModifyDto modifyDto) {
		IdentityCardBean bean=new IdentityCardBean();
		BeanUtils.copyProperties(modifyDto, bean);
		int num = identityCardMapper.modifyIdNumber(bean);
		return num == 1 ;
	}
	
	/**
	 * 修改用户身份证号信息
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyIdNumberAddress(IdentityCardModifyDto modifyDto) {
		IdentityCardBean bean=new IdentityCardBean();
		BeanUtils.copyProperties(modifyDto, bean);
		int num = identityCardMapper.modifyIdNumberAddress(bean);
		return num == 1 ;
	}

	 /**
	   * 获取用户身份证信息
	   * @param idNumber 用户省份证号
	   * @return
	   */	
	@Override
	public IdentityCardDto getIdentityCard(String idNumber) {
		
		IdentityCardBean identityCard = identityCardMapper.getIdentityCard(idNumber);
		
		IdentityCardDto dto = Convert.toIdentityCardDto(identityCard);
		
		return dto;
	}

}
